package appapi.dao;

import appapi.entity.Zhongjiao.StaffDto;
import appapi.entity.mqtt.Terminal;
import appapi.entity.mqtt.Terminals;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Ygp Work
 * @description:
 * @date: 2023-07-04 10:56
 */
@Mapper
public interface TerminalMapper extends BaseMapper<Terminal> {
    @Insert("INSERT INTO terminal (term_id, term_no, term_topic, term_data, create_oper, update_oper, create_time, update_time) " +
            "VALUES (#{termId}, #{termNo}, #{termTopic}, #{termData}, #{createOper}, #{updateOper}, #{createTime}, #{updateTime})")
    int insertTerminal(Terminal terminal);

    @Delete("DELETE FROM terminal WHERE term_id = #{termId}")
    int deleteTerminalById(String termId);

    @Update("UPDATE terminal SET term_no = #{termNo}, term_topic = #{termTopic}, term_data = #{termData}, " +
            "create_oper = #{createOper}, update_oper = #{updateOper}, create_time = #{createTime}, update_time = #{updateTime} " +
            "WHERE term_id = #{termId}")
    int updateTerminal(Terminal terminal);

    @Select("SELECT term_id,term_no,term_topic,term_data FROM terminal WHERE term_no = #{termNo} AND term_topic = #{termTopic}")
    Terminal getTerminalByOnAndTopic(@Param("termNo") String termNo, @Param("termTopic") String termTopic);

    @Select("SELECT term_id,term_no,term_topic,term_data FROM terminal WHERE term_no = #{termNo} AND term_topic = #{termTopic} order by create_time desc ")
    List<Terminal> getTerminalByOnAndTopics(@Param("termNo") String termNo, @Param("termTopic") String termTopic);

    @Select({
            "<script>",
            "SELECT term_no, term_data FROM terminal WHERE term_topic = 'reginfo'",
            "<if test='termNo != null and termNo != \"\"'>",
            "AND term_no LIKE CONCAT('%', #{termNo}, '%')",
            "</if>",
            "<if test='termData != null and termData != \"\"'>",
            "AND term_data LIKE CONCAT('%', #{termData}, '%')",
            "</if>",
            "</script>"
    })
    List<Terminal> getTerminalAll(Terminal terminal);


//    @Select({
//            "<script>",
//            "SELECT term_no, term_data FROM terminal WHERE term_topic = 'reginfo'",
//            "<if test='termNo != null and termNo != \"\"'>",
//            "AND term_no LIKE CONCAT('%', #{termNo}, '%')",
//            "</if>",
//            "<if test='termData != null and termData != \"\"'>",
//            "AND term_data LIKE CONCAT('%', #{termData}, '%')",
//            "</if>",
//            "</script>"
//    })
//    List<Terminals> getTerminalAlls(Terminals terminal);


    @Select({
            "<script>",
            "SELECT term_no, term_data FROM terminal WHERE term_topic = 'reginfo'",
            "<if test='termNo != null and termNo != \"\"'>",
            "AND term_no LIKE CONCAT('%', #{termNo}, '%')",
            "</if>",
            "<if test='termData != null and termData != \"\"'>",
            "AND term_data LIKE CONCAT('%', #{termData}, '%')",
            "</if>",
            "ORDER BY CASE WHEN term_data IS NULL OR term_data = '' THEN 1 ELSE 0 END, term_data ASC",
            "</script>"
    })
    List<Terminals> getTerminalAlls(Terminals terminal);


    @Select("select staff_mobile,id_card_no from staff")
    List<StaffDto> getUserAll();
}
